#include "twothousandfortyeight.h"
#include "ui_twothousandfortyeight.h"

TwoThousandFortyEight::TwoThousandFortyEight(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::TwoThousandFortyEight)
{
    ui->setupUi(this);
    Init();

    srand((unsigned int)time (NULL));
    connect(button,SIGNAL(clicked()),this,SLOT(slotStart()));
}
TwoThousandFortyEight::~TwoThousandFortyEight()
{
    delete ui;
}
//绘图
void TwoThousandFortyEight::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setBrush(Qt::blue);
    painter.setFont(QFont("微软雅黑",20,700,false));       //设置字体___________________________
    QString strscore;
    painter.drawText(QPoint(20,60),"得分："+QString::number(score));    //显示得分(需要将int ->QString)

    //画格子
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            painter.setPen(Qt::transparent);
            if(s[i][j]==0){
                painter.setBrush(Qt::gray);
                painter.drawRect(i*60+40,j*60+120,55,55);
            }
            else if(s[i][j]==2){
                painter.setBrush(Qt::red);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2),QTextOption(Qt::AlignCenter));   //在矩形中写文字
            }
            else if(s[i][j]==4){
                painter.setBrush(Qt::darkRed);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(4),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==8){
                painter.setBrush(Qt::green);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(8),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==16){
                painter.setBrush(Qt::darkGreen);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(16),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==32){
                painter.setBrush(Qt::yellow);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(32),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==64){
                painter.setBrush(Qt::darkYellow);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(64),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==128){
                painter.setBrush(Qt::cyan);     //蓝绿色
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(128),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==256){
                painter.setBrush(Qt::darkCyan);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(256),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==512){
                painter.setBrush(Qt::magenta);  //品红
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(512),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==1024){
                painter.setBrush(Qt::darkMagenta);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(1024),QTextOption(Qt::AlignCenter));
            }
            else if(s[i][j]==2048) {
                painter.setBrush(Qt::blue);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(2048),QTextOption(Qt::AlignCenter));
            }
            else  {
                painter.setBrush(Qt::darkBlue);
                painter.drawRect(i*60+40,j*60+120,55,55);
                painter.setPen(Qt::black);
                painter.setFont(QFont("微软雅黑",10,700,false));
                painter.drawText(QRectF(i*60+40,j*60+120,55,55),QString::number(s[i][j]),QTextOption(Qt::AlignCenter));
            }
        }
    }
}
//键盘事件
void TwoThousandFortyEight::keyPressEvent(QKeyEvent *event)
{
    switch(event->key())
    {
    case Qt::Key_W:
        PressUp();
        break;
    case Qt::Key_S:
        PressDown();
        break;
    case Qt::Key_A:
        PressLeft();
        break;
    case Qt::Key_D:
        PressRight();
        break;
    default:;
    }
    myRand();
    update();//强制刷新

}
//初始化
void TwoThousandFortyEight::Init()
{
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }
    score=0;
    button=new QPushButton("开始游戏！",this);
    button->setGeometry(60,400,200,50);
}

void TwoThousandFortyEight::PressUp()
{
    //移动
    for(int i=0;i<4;i++)
    {
        for(int j=1;j<4;j++)
        {
            if(s[i][j]==0)
                continue;
            for(int p=0;p<j;p++)
            {
                if(s[i][p]==0){
                    s[i][p]=s[i][j];
                    s[i][j]=0;
                     break;
                }
            }
        }
    }
    //相加
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<3;j++)
        {
            if(s[i][j]==s[i][j+1])
            {
                s[i][j]*=2;
                s[i][j+1]=0;
                score+=s[i][j];
                for(int p=j+2;p<4;p++)
                {
                    s[i][p-1]=s[i][p];
                }
            }
        }
    }
}

void TwoThousandFortyEight::PressDown()
{

}
void TwoThousandFortyEight::PressLeft()
{

}

void TwoThousandFortyEight::PressRight()
{

}

void TwoThousandFortyEight::myRand()
{
    int i=0,j=0;
    Ns n[16];
    int ni=0;   //空格子个数
    for(int i=0;i<4;i++)
    {
        for(int j=0;j<4;j++)
        {
            if(s[i][j]==0)
            {
                n[ni].i=i;
                n[ni].j=j;
                ni++;
            }

        }
    }
    //判断游戏是否结束
    if(ni==0)
    {
        for(int i=0;i<4;i++)
        {
            for(int j=0;j<3;j++)
            {
                if(s[i][j]==s[i][j+1])
                    return;
            }
        }
        for(int j=0;j<4;j++)
        {
            for(int i=0;i<3;i++)
            {
                if(s[i][j]==s[i+1][j])
                    return;
            }
        }
        QMessageBox::about(this,"游戏结束","得分："+QString::number(score));
        return;
    }
    int random=rand()%ni;
    s[n[random].i][n[random].j]=2*(rand()%2+1);

}
//按钮的槽函数
void TwoThousandFortyEight::slotStart()
{
    QMessageBox::about(this,"Rule","上下左右控制方向");
    score=0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            s[i][j]=0;
        }
    }
    button->setText("重新开始！");
    int randi=rand()%4;
    int randj=rand()%4;
    s[randi][randj]=2;
    state=true;
    update();
}





